package com.adee.gulimall.search.web;

import com.adee.gulimall.search.service.MallSearchService;
import com.adee.gulimall.search.vo.SearchParam;
import com.adee.gulimall.search.vo.SearchResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
public class IndexController {
    @Autowired
    MallSearchService mallSearchService;

    @GetMapping({"/", "/index.html"})
    public String index() {
        return "index";
    }

    @GetMapping({"/list.html"})
    public String listPage(SearchParam param, Model model) {
        // 根据页面传递过来的查询参数，去es中检索商品
        try {
            SearchResult result = mallSearchService.search(param);
            model.addAttribute("result", result);
        } catch (Exception e) {
            log.error("es检索商品报错", e);
        }
        return "list";
    }


    // 简单服务，不涉及到后后端，即redis、mysql，用于压测：nginx -> gateway -> 简单服务
    @GetMapping("/search/hello")
    @ResponseBody
    public String hello() {
        return "hello";
    }
}
